﻿using Communication.Message.Buffer;
using Communication.Message.Osc;
using growatt.Message.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace growatt.Message.Osc
{
    public class ConfigDataChanelMessage : OscChannelMessage

    /// <summary>
    /// 地址设置报文处理
    /// </summary>
    {
        public ConfigDataChanelMessage(int[] addrs, byte SignalType, byte SignalRatio, int valueToSet) : base(0x1F)
        {
            try
            {
                this.channelCount = addrs.Length;
                this.sendBuffer = MessageBuffers.Buffer(13);
                this.receiveBuffer = MessageBuffers.DynamicBuffer(13);
                this.sendBuffer.WriteByte(byte.MaxValue);
                this.sendBuffer.WriteByte(this.fun);
                for (int i = 0; i < addrs.Length; i++)
                {
                    this.sendBuffer.WriteInt(addrs[i]);
                }
                var ty = SignalType;
                if (ty == 0 || ty == 1)
                {
                    this.sendBuffer.WriteByte(0);
                }
                else
                {
                    this.sendBuffer.WriteByte(1);
                }
                this.sendBuffer.WriteByte(SignalRatio);
                if (SignalType == 1)
                {
                    var valueToSetshort = short.Parse(valueToSet.ToString());
                    var bytevalueToSet = BitConverter.GetBytes(valueToSetshort).Reverse().ToList();
                    this.sendBuffer.WriteByte(0);
                    this.sendBuffer.WriteByte(0);
                    for (global::System.Int32 i = 0; i < bytevalueToSet.Count; i++)
                        this.sendBuffer.WriteByte(bytevalueToSet[i]);
                }
                else
                {
                    var bytevalueToSet = BitConverter.GetBytes(valueToSet).Reverse().ToList();
                    for (global::System.Int32 i = 0; i < bytevalueToSet.Count; i++)
                        this.sendBuffer.WriteByte(bytevalueToSet[i]);
                }
                byte b = OscUtils.Crc8(this.sendBuffer.Array(), this.sendBuffer.ReaderIndex(), this.sendBuffer.ReadableBytes());
                this.sendBuffer.WriteByte(b);
                var str = ConvertHelper.ByteToHexStr(sendBuffer.Array(), joinString: " ");
                this.sendBuffer.MarkReaderIndex();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        /// <summary>
        /// 处理接收报文
        /// </summary>
        /// <returns></returns>
        protected override bool DealwithReceiveMsg()
        {
            this.errorCode = OscErrorCode.SUCCESS;
            base.SetSuccess();
            return true;
        }

        public override int GetCancelSendData(byte[] buffer, int offset)
        {
            return 0;
        }

        private int channelCount;
    }
}

